package k8s_ui

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"k8s-platform/service/k8s-ui"
	"net/http"
)

// 获取deployment列表，支持过滤、排序、分页
var Deployment deployment

type deployment struct{}

func (*deployment) GetDeployments(ctx *gin.Context) {
	params := new(struct {
		Pool       string `form:"pool"`
		FilterName string `form:"filter_name"`
		Namespace  string `form:"namespace"`
		Page       int    `form:"page"`
		Limit      int    `form:"limit"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	data, err := k8s_ui.Deployment.GetDeployments(params.FilterName, params.Namespace, params.Limit, params.Page)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取Deployment列表成功",
		"data": data,
		"pool": pool,
	})
}

// 获取deployment详情
func (*deployment) GetDeploymentDetail(ctx *gin.Context) {
	params := new(struct {
		Pool           string `form:"pool"`
		DeploymentName string `form:"deployment_name"`
		Namespace      string `form:"namespace"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	data, err := k8s_ui.Deployment.GetDeploymentDetail(params.DeploymentName, params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取Deployment详情成功",
		"data": data,
		"pool": pool,
	})
}

// 创建deployment
func (d *deployment) CreateDeployment(ctx *gin.Context) {
	var (
		deployCreate = new(k8s_ui.DeployCreate)
		err          error
	)

	//params := new(struct {
	//	Pool string `form:"pool"`
	//})

	if err = ctx.ShouldBindJSON(deployCreate); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(deployCreate.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}
	err_Deployment := k8s_ui.Deployment.CreateDeployment(deployCreate)
	if err_Deployment != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_Deployment.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建Deployment成功",
		"data": nil,
		"pool": pool,
	})
}

func (d *deployment) CreateDeployment_test(ctx *gin.Context) {
	//var (
	//	deployCreate = new(k8s_ui.DeployCreate)
	//	err          error
	//)
	//
	//if err = ctx.ShouldBindJSON(deployCreate); err != nil {
	//	logger.Error("Bind请求参数失败, " + err.Error())
	//	ctx.JSON(http.StatusInternalServerError, gin.H{
	//		"msg":  err.Error(),
	//		"data": nil,
	//	})
	//	return
	//}

	_, err_pool := k8s_ui.K8s.IntPool("本地mysqlk8s")
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}
	err_Deployment := k8s_ui.Deployment.CreateDeployment_test()
	if err_Deployment != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_Deployment.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "创建Deployment成功",
		"data": "test请求创建成功但并不代表创建成功",
		"pool": "pool",
	})
}

// 设置deployment副本数
func (d *deployment) ScaleDeployment(ctx *gin.Context) {
	params := new(struct {
		Pool           string `form:"pool"`
		DeploymentName string `json:"deployment_name"`
		Namespace      string `json:"namespace"`
		ScaleNum       int    `json:"scale_num"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	pool, err := k8s_ui.K8s.IntPool(params.Pool)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	data, err := k8s_ui.Deployment.ScaleDeployment(params.DeploymentName,
		params.Namespace, params.ScaleNum)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "设置Deployment副本数成功",
		"data": fmt.Sprintf("最新副本数: %d", data),
		"pool": pool,
	})
}

// 删除deployment
func (d *deployment) DeleteDeployment(ctx *gin.Context) {
	params := new(struct {
		Pool           string `form:"pool"`
		DeploymentName string `json:"deployment_name"`
		Namespace      string `json:"namespace"`
	})
	//DELETE请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	err := k8s_ui.Deployment.DeleteDeployment(params.DeploymentName,
		params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除Deployment成功",
		"data": nil,
		"pool": pool,
	})
}

// 重启deployment
func (d *deployment) RestartDeployment(ctx *gin.Context) {
	params := new(struct {
		Pool           string `form:"pool"`
		DeploymentName string `json:"deployment_name"`
		Namespace      string `json:"namespace"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	err := k8s_ui.Deployment.RestartDeployment(params.DeploymentName,
		params.Namespace)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "重启Deployment成功",
		"data": nil,
		"pool": pool,
	})
}

// 更新deployment
func (d *deployment) UpdateDeployment(ctx *gin.Context) {
	params := new(struct {
		Pool      string `form:"pool"`
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	//PUT请求，绑定参数方法改为ctx.ShouldBindJSON
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error("Bind请求参数失败, " + err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	pool, err_pool := k8s_ui.K8s.IntPool(params.Pool)
	if err_pool != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err_pool.Error(),
			"data": nil,
		})
		return
	}

	err := k8s_ui.Deployment.UpdateDeployment(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新Deployment成功",
		"data": nil,
		"pool": pool,
	})
}

// 获取每个namespace的deployment数量
func (d *deployment) GetDeployNumPerNp(ctx *gin.Context) {

	data, err := k8s_ui.Deployment.GetDeployNumPerNp()
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取每个namespace的deployment数量成功",
		"data": data,
	})
}
